package com.yiboshi.rehrs.domain.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.yiboshi.rehrs.common.VarConstant;
import com.yiboshi.rehrs.domain.entity.InviteRecord;
import com.yiboshi.rehrs.domain.entity.SmZhongUser;
import com.yiboshi.rehrs.domain.entity.ZhongUser;
import com.yiboshi.rehrs.domain.entity.ZhongUserImage;
import com.yiboshi.rehrs.domain.mapper.ZhongUserMapper;
import com.yiboshi.rehrs.domain.service.IInviteRecordService;
import com.yiboshi.rehrs.domain.service.ISystemConfigService;
import com.yiboshi.rehrs.domain.service.IZhongUserImageService;
import com.yiboshi.rehrs.domain.service.IZhongUserService;
import com.yiboshi.rehrs.param.ZhongReq;
import com.yiboshi.rehrs.util.RechargeCardUtil;
import com.yiboshi.rehrs.util.StrUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author local
 * @since 2025-03-17
 */
@Service
@Slf4j
public class ZhongUserServiceImpl extends AperBaseServiceImpl<ZhongUserMapper, ZhongUser> implements IZhongUserService {



    @Autowired
    IZhongUserImageService zhongUserImageService;

    @Autowired
    IInviteRecordService inviteRecordService;

    @Autowired
    ISystemConfigService systemConfigService;


    @Override
    public boolean saveOrUpdateUserFace(ZhongReq entity) {
        String data = entity.getData();
        String account = entity.getAccount();
        if(StrUtils.isBlank(data)){
            return false;
        }
        return this.saveOrUpdateUserFace(account,data);
    }
    @Override
    public ZhongUser one(String account) {
        return this.getOne(account,0);
    }

    @Override
    public ZhongUser getOne(String account, Integer mode) {
        LambdaQueryWrapper<ZhongUser> queryWrapper = this.getQueryWrapper()
                .eq(ZhongUser::getAccount, account)
                .eq(ZhongUser::getMode, mode)
                .eq(ZhongUser::getDeleted,0)
                ;
        ZhongUser one = this.getOne(queryWrapper);
        return one;
    }

    @Override
    public ZhongUser getSmOne(String account) {
        return getOne(account,1);
    }

    @Override
    public ZhongUser getByCode(String userCode, Integer mode) {
        LambdaQueryWrapper<ZhongUser> queryWrapper = this.getQueryWrapper()
                .eq(ZhongUser::getUserCode, userCode)
                .eq(ZhongUser::getMode, mode)
                .eq(ZhongUser::getDeleted,0)
                ;
        ZhongUser one = this.getOne(queryWrapper);
        return one;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateUserFace(ZhongUser zhongUser, ZhongUserImage zhongUserImage) {

        this.saveOrUpdateUser(zhongUser);
        if(zhongUserImage!=null){
            this.saveOrUpdateUserFace(zhongUserImage.getAccount(),zhongUserImage.getImage());
        }
    }

    @Override
    public List<ZhongUser> autoTaskAccount(int autoStatus) {
        LambdaQueryWrapper<ZhongUser> queryWrapper = getQueryWrapper()
                .eq(ZhongUser::getMode,0)
                .eq(ZhongUser::getDeleted,0)
                .eq(ZhongUser::getAutoStatus, autoStatus)
                .ge(ZhongUser::getAutoTaskType,1)
                .ge(ZhongUser::getVipExpDate, LocalDateTime.now())
                ;
        return list(queryWrapper);
    }

    @Override
    public List<ZhongUser> todayOutVip(int autoStatus,String agentAccount) {
        LocalDate now = LocalDate.now();
        LocalDate tomorrow = now.plusDays(2);
        LambdaQueryWrapper<ZhongUser> queryWrapper = getQueryWrapper()
                .eq(ZhongUser::getMode,0)
                .eq(ZhongUser::getAgentAccount, agentAccount)
                .eq(ZhongUser::getAutoStatus, autoStatus)
                .ge(ZhongUser::getVipExpDate, now.atStartOfDay())
                .le(ZhongUser::getVipExpDate, tomorrow.atStartOfDay())
                .orderByAsc(ZhongUser::getVipExpDate)
                ;
        return list(queryWrapper);
    }

    @Override
    public SmZhongUser getSmUser(String account) {
        ZhongUser one = this.getOne(account, 1);

        if(Objects.isNull(one)){
            return null;
        }
        SmZhongUser smZhongUser = new SmZhongUser();
        BeanUtils.copyProperties(one, smZhongUser);

        int chargeDays = systemConfigService.exchangeVipDays();
        //chargeDays = 0;// TODO 暂时屏蔽邀请
        smZhongUser.setChargeDays(chargeDays);
        if(chargeDays==0){
            return smZhongUser;
        }
        LambdaQueryWrapper<InviteRecord> queryWrapper = inviteRecordService
                .getQueryWrapper().eq(InviteRecord::getUsedAccount, account);
        //是否可以提交邀请码 1可以 2不可以
        smZhongUser.setEnableCharge(inviteRecordService.count(queryWrapper)<=0?1:2);
        List<InviteRecord> list =inviteRecordService.listOriginAccount(account);
        int total = 0;
        int unrealized = 0;
        if(CollUtil.isNotEmpty(list)){
            for (InviteRecord inviteRecord : list) {
                total = total+inviteRecord.getCount();
                Integer originStatus = inviteRecord.getOriginStatus();
                if(Objects.equals(originStatus, 0)){
                    unrealized = unrealized+inviteRecord.getVipDays();
                }
            }
        }
        smZhongUser.setTotalNum(total);
        smZhongUser.setUnrealizedNum(unrealized);
        return smZhongUser;

    }

    @Override
    public boolean saveOrUpdateUser(ZhongUser entity) {

        LambdaQueryWrapper<ZhongUser> eq = this.getQueryWrapper().eq(ZhongUser::getAccount, entity.getAccount())
                .eq(ZhongUser::getMode, entity.getMode()).eq(ZhongUser::getDeleted,0);
        ZhongUser one = this.getOne(eq);
        if (one != null) {
            entity.setVipExpDate(null);
            entity.setId(one.getId());
            boolean res = this.updateById(entity);
            entity.setVipExpDate(one.getVipExpDate());
            return  res;
        } else {
            entity.setUserCode(RechargeCardUtil.inviteCode());
            entity.setId(IdWorker.getId());
            entity.setVipExpDate(LocalDateTime.now());
            return this.save(entity);
        }
    }

    public boolean saveOrUpdateUserFace(String account,String faceImage) {
        if(StrUtils.isBlank(faceImage)){
            return false;
        }
        LambdaUpdateWrapper<ZhongUserImage> updateWrapper = zhongUserImageService.getUpdateWrapper().eq(ZhongUserImage::getAccount, account)
                .set(ZhongUserImage::getImage, faceImage)
                .set(ZhongUserImage::getUpdateTime, LocalDateTime.now());
        boolean update = zhongUserImageService.update(updateWrapper);
        if(!update){
            ZhongUserImage zhongUserImage = new ZhongUserImage();
            zhongUserImage.setAccount(account);
            zhongUserImage.setImage(faceImage);
            zhongUserImage.setId(IdWorker.getId());
            zhongUserImage.setUpdateTime(LocalDateTime.now());
            update = zhongUserImageService.save(zhongUserImage);
        }
        if(!update){
            log.error("account:{} 更新图片失败",account);
        }
        return update;
    }
}
